package unclassified.ccb;

import java.util.Arrays;

public class HistogramAnalyse {

    public int[] analysisHistogram(int[] heights, int cnt) {
        Arrays.sort(heights);
        int l = 0, r = cnt - 1;
        int indexMin = l;
        while (r < heights.length) {
            int f1 = heights[indexMin + cnt - 1] - heights[indexMin];
            int f2 = heights[r] - heights[l];
            indexMin = f1 <= f2 ? indexMin : l;
            l++;
            r++;
        }
        int[] res = new int[cnt];
        int index = 0;
        while(index < cnt) {
            res[index++] = heights[indexMin++];
        }
        return res;
    }

    public static void main(String[] args) {
        HistogramAnalyse ha = new HistogramAnalyse();
        ha.analysisHistogram(new int[]{3,2,7,6,1,8}, 3);
    }
}
